<template>
  <div>
    <Card>
      <div slot="title">
        <div class="edit-head">
          <a @click="close" class="back-title">
            <Icon type="ios-arrow-back" />返回
          </a>
          <div class="head-name">添加</div>
          <span></span>
          <a @click="close" class="window-close">
            <Icon type="ios-close" size="31" class="ivu-icon-ios-close" />
          </a>
        </div>
      </div>
      <Form
        ref="form"
        :model="form"
        :label-width="100"
        :rules="formValidate"
        label-position="left"
      >
        <FormItem label="险种编码" prop="productCode">
          <Input v-model="form.productCode" clearable style="width: 570px" />
        </FormItem>
        <FormItem label="产品简称" prop="productAbbreviation">
          <Input
            v-model="form.productAbbreviation"
            clearable
            style="width: 570px"
          />
        </FormItem>
        <FormItem label="产品编码" prop="productClassCode">
          <Select
            v-model="form.productClassCode"
            clearable
            style="width: 570px"
          >
            <!-- <Option v-for="item in insuranceType" :key="item.id" :label="item.title" :value="item.id">{{ item.title }}</Option> -->
            <Option
              v-for="item in insuranceType"
              :key="item.id"
              :value="item.value"
              >{{ item.title }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="是否有效" prop="enabled">
          <!-- <RadioGroup v-model="form.enabled">
            <Radio label="0">否</Radio>
            <Radio label="1">是</Radio>
          </RadioGroup> -->
          <i-switch v-model="form.enabled" @on-change="change"></i-switch>
        </FormItem>
        <FormItem label="产品价格" prop="price">
          <InputNumber
            v-model="form.price"
            min="0"
            max="5000000"
            style="width: 570px"
          ></InputNumber>
        </FormItem>
        <FormItem label="详细描述" prop="details">
          <Input v-model="form.details" clearable style="width: 570px" />
        </FormItem>
        <FormItem label="产品名称" prop="productName">
          <Input v-model="form.productName" clearable style="width: 570px" />
        </FormItem>
        <Form-item class="br">
          <Button @click="handleSubmit" :loading="submitLoading" type="primary"
            >提交并保存</Button
          >
          <Button @click="handleReset">重置</Button>
          <Button type="dashed" @click="close">关闭</Button>
        </Form-item>
      </Form>
    </Card>
  </div>
</template>

<script>
import { addOrEditInsurance, getInsuranceType } from "./api.js";
export default {
  name: "add",
  components: {},
  data() {
    return {
      submitLoading: false, // 表单提交状态
      form: {
        // 添加或编辑表单对象初始化数据
        productCode: "",
        productAbbreviation: "",
        productClassCode: "",
        enabled: true,
        price: 0,
        details: "",
        productName: "",
      },
      // 表单验证规则
      formValidate: {
        productCode: [{ required: true, message: "不能为空", trigger: "blur" }],
        productAbbreviation: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        productClassCode: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        // enabled: [{ required: true, message: "不能为空", trigger: "blur" }],
        price: [
          {
            type: "number",
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        details: [{ required: true, message: "不能为空", trigger: "blur" }],
        productName: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
      insuranceType: [],
    };
  },
  methods: {
    // 开关
    change(status) {
      // this.$Message.info("开关状态：" + status);
      console.log(status);
      this.form.enabled = status;
    },
    init() {
      getInsuranceType({ keyword: "险种类型" }).then((res) => {
        this.insuranceType = res.result;
      });
    },
    handleReset() {
      this.$refs.form.resetFields();
    },
    handleSubmit() {
      // this.$refs.form.validate((valid) => {
      //   if (valid) {
      console.log("提交");
      addOrEditInsurance(this.form).then((res) => {
        this.submitLoading = false;
        if (res.success) {
          this.$Message.success("操作成功");
          this.submited();
        }
      });
      //   }
      // });
    },
    close() {
      this.$emit("close", true);
    },
    submited() {
      this.$emit("submited", true);
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/single-common.less";
.edit-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;

  .back-title {
    color: #515a6e;
    display: flex;
    align-items: center;
  }

  .head-name {
    display: inline-block;
    height: 20px;
    line-height: 20px;
    font-size: 16px;
    color: #17233d;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .window-close {
    z-index: 1;
    font-size: 12px;
    position: absolute;
    right: 0px;
    top: -5px;
    overflow: hidden;
    cursor: pointer;

    .ivu-icon-ios-close {
      color: #999;
      transition: color 0.2s ease;
    }
  }

  .window-close .ivu-icon-ios-close:hover {
    color: #444;
  }
}
</style>